/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.reporting;

import TimeRecording.business.DateRange;
import TimeRecording.tables.Employee;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Map;
import javax.persistence.EntityManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author david
 */
public class AnomaliesReport {
    
    private final static String REPORT_NAME = "/reports/employee/AnomaliesReport.jasper";
     
    // <editor-fold defaultstate="collapsed" desc="TimeDetail">
    public static class TimeDetail {
        
        private Date date;
        private double hoursExpected;
        private double hoursRecorded;
        
        public TimeDetail (){           
        }
        
        public TimeDetail (Date date, double hoursExpected, double hoursRecorded){            
            this.date = date;
            this.hoursExpected = hoursExpected;
            this.hoursRecorded = hoursRecorded;                 
        }
        
        public Date getDate(){
            return date;
        }
        
        public void setDate(Date date){
            this.date = date;
        }
        
        public double getHoursExpected(){
            return hoursExpected;
        }
        
        public void setHoursExpected(double hoursExpected){
            this.hoursExpected = hoursExpected;
        }
        
        public double getHourRecorded(){
            return hoursRecorded;
        }
            
        public void setHoursRecorded(double hoursRecorded){
            this.hoursRecorded = hoursRecorded;
        }
        
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="EmployeeDetail">
    public static class EmployeeDetail {
        
        
        private String name;
        private double missingTimeEntries;
        private double unapprovedHoliday;
        private double unacknowledgedSick;
        private double openSickness;
        
        private JRBeanCollectionDataSource missingTimeEntriesReport;
        private JRBeanCollectionDataSource unapprovedHolidayReport;
        private JRBeanCollectionDataSource unacknowledgedSickReport;
        private JRBeanCollectionDataSource openSickReport;
    
        public EmployeeDetail() {            
        }
        
        public EmployeeDetail(String name, 
                              double missingTimeEntries, 
                              double unapprovedHoliday,
                              double unacknowledgedSick,
                              double openSickness,
                              JRBeanCollectionDataSource missingTimeEntriesReport,
                              JRBeanCollectionDataSource unapprovedHolidayReport,
                              JRBeanCollectionDataSource unacknowledgedSickReport,
                              JRBeanCollectionDataSource openSickReport){
            
            this.name = name;
            this.missingTimeEntries = missingTimeEntries;
            this.unapprovedHoliday = unapprovedHoliday;
            this.unacknowledgedSick = unacknowledgedSick;
            this.openSickness = openSickness;
            
            this.missingTimeEntriesReport = missingTimeEntriesReport;
            this.unapprovedHolidayReport = unapprovedHolidayReport;
            this.unacknowledgedSickReport = unacknowledgedSickReport;
            this.openSickReport = openSickReport;        
        }
        
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }    
        
        public double getMissingTimeEntries(){
            return missingTimeEntries;       
        }
        
        public void setMissingTimeEntries (double missingTimeEntries){
            this.missingTimeEntries = missingTimeEntries;
        }
        
        public double getUnapprovedHoliday(){
            return unapprovedHoliday;       
        }
        
        public void setUnapprovedHoliday (double unapprovedHoliday){
            this.unapprovedHoliday = unapprovedHoliday;
        }
        
        public double getUnacknowledgedSick (){
            return unacknowledgedSick;       
        }
        
        public void setUnacknowledgedSick (double unacknowledgedSick){
            this.unacknowledgedSick = unacknowledgedSick;
        }
        
        public double getOpenSickness (){
            return openSickness;       
        }
        
        public void setOpenSickness (double openSickness){
            this.openSickness = openSickness;
        }
        
        public JRBeanCollectionDataSource getMissingTimeEntriesReport() {
            return missingTimeEntriesReport;
        }
        
        public void setMissingTimeEntriesReport(JRBeanCollectionDataSource missingTimeEntriesReport) {
            this.missingTimeEntriesReport = missingTimeEntriesReport;
        }
       
        public JRBeanCollectionDataSource getUnapprovedHolidayReport() {
            return unapprovedHolidayReport;
        }
        
        public void setUnapprovedHolidayReport(JRBeanCollectionDataSource unapprovedHolidayReport) {
            this.unapprovedHolidayReport = unapprovedHolidayReport;
        }
        
        public JRBeanCollectionDataSource getUnacknowledgedSickReport() {
            return unacknowledgedSickReport;
        }
        
        public void setUnacknowledgedSickReport(JRBeanCollectionDataSource unacknowledgedSickReport) {
            this.unacknowledgedSickReport = unacknowledgedSickReport;
        }
        
        public JRBeanCollectionDataSource getOpenSickReport() {
            return openSickReport;
        }
        
        public void setOpenSickReport(JRBeanCollectionDataSource openSickReport) {
            this.openSickReport = openSickReport;
        }
        
    }  
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="viewReport">
    @SuppressWarnings("unchecked")
    public void viewReport(EntityManager entityManager, Date startDate, Date endDate) {
              
        List<Employee> employees = Employee.getActiveEmployees(entityManager);
        
        viewReport(employees, entityManager, startDate, endDate);
    }
    // </editor-fold>
    
    @SuppressWarnings("unchecked")
    public void viewReport(Collection<Employee> employeesIn, EntityManager entityManager, Date startDate, Date endDate) {
        
        ArrayList<EmployeeDetail> data = new ArrayList<EmployeeDetail>();
                           
        Map parameters = new ReportingUtilities().getBasicParameters(); 
   
        DateRange dateRange = new DateRange();
        
        dateRange.setFromDate(startDate);
        dateRange.setToDate(endDate);
        
        for (Employee employeeIn : employeesIn) {
            
            EmployeeDetail employeeOut = new EmployeeDetail();
                         
            employeeOut.setName(employeeIn.getFriendlyName());
            employeeOut.setMissingTimeEntries(0);
            employeeOut.setUnapprovedHoliday(0);
            employeeOut.setUnacknowledgedSick(0);
            employeeOut.setOpenSickness(0);
            
            employeeOut.setMissingTimeEntriesReport(new JRBeanCollectionDataSource(processMissingTimesheetEntries(employeeIn, dateRange)));            
            employeeOut.setUnapprovedHolidayReport(new JRBeanCollectionDataSource(processUnapprovedHoliday(employeeIn, dateRange)));
            employeeOut.setUnacknowledgedSickReport(new JRBeanCollectionDataSource(processUacknowledgedSick(employeeIn, dateRange)));
            employeeOut.setOpenSickReport(new JRBeanCollectionDataSource(processOpenSick(employeeIn, dateRange)));
            
            data.add(employeeOut);
        }
                        
        try {                         
            JasperReport template = (JasperReport)JRLoader.loadObject(getClass().getResource(REPORT_NAME));                    
            JasperPrint report = JasperFillManager.fillReport(template, parameters, new JRBeanCollectionDataSource(data));            
            JasperViewer.viewReport(report, false);
            
        } catch (JRException ex) {
            Logger.getLogger(EndOfMonthSummary.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private Collection<TimeDetail> processMissingTimesheetEntries(Employee employee, DateRange dateRange) {                                                        
        return null;
    }
    
    private Collection<EmployeeDetail> processUnapprovedHoliday(Employee employee, DateRange dateRange) {
        return null;
    }
    
    private Collection<EmployeeDetail> processUacknowledgedSick(Employee employee, DateRange dateRange) {
        return null;
    }
    
    private Collection<EmployeeDetail> processOpenSick(Employee employee, DateRange dateRange) {
        return null;
    }
}
